package com.onlinevideo.dao.impl;

import com.onlinevideo.dao.BaseDao;

import com.onlinevideo.dao.HistoryDao;
import com.onlinevideo.entity.History;

import java.util.List;

public class HistoryDaoImpl extends BaseDao implements HistoryDao {
    @Override
    public int insert(History history) {
        String sql = "INSERT INTO `T_history` (`user_id`,`video_id`,`time`) VALUES (?,?,?)";
        return super.update(sql, history.getUserId(), history.getVideoId(), history.getTime());
    }

    @Override
    public int delete(Long id) {
        String sql = "DELETE FROM `T_history` WHERE `id`=?";
        return super.update(sql, id);
    }

    @Override
    public int deleteByUserId(Long userId) {
        String sql = "DELETE FROM `T_history` WHERE `user_id`=? ";//AND `time`<"+(System.currentTimeMillis()-1000*60*60)
        return super.update(sql, userId);
    }

    @Override
    public int deleteByVideoId(Long videoId) {
        String sql = "DELETE FROM `T_history` WHERE `video_id`=?";
        return super.update(sql, videoId);
    }



    @Override
    public int update(History history) {
        String sql = "UPDATE `T_history` SET `user_id`=?,`video_id`=?,`time`=? WHERE `id`=?";
        return super.update(sql,history.getUserId(), history.getVideoId(), history.getTime(),history.getId());
    }

    @Override
    public History queryById(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`video_id` AS `videoId`,`time` FROM `T_history` WHERE `id`=?";
        return super.queryForOne(History.class,sql,id);
    }

    @Override
    public List<History> queryAll() {
        String sql = "SELECT `id`,`user_id` AS `userId`,`video_id` AS `videoId`,`time` FROM `T_history`";
        return super.queryForList(History.class,sql);
    }

    @Override
    public List<History> queryByUser(Long id) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`video_id` AS `videoId`,`time` FROM `T_history` WHERE `user_id`=? ORDER BY `time` DESC";
        return super.queryForList(History.class,sql,id);
    }

    @Override
    public List<History> queryByUserAndVideo(Long userId, Long videoId) {
        String sql = "SELECT `id`,`user_id` AS `userId`,`video_id` AS `videoId`,`time` FROM `T_history` WHERE `user_id`=? AND `video_id`=?";
        return super.queryForList(History.class,sql,userId,videoId);
    }
}
